package com.example.as_sum.expand;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.as_sum.R;
import com.example.as_sum.entity.ShopInfo;
import com.example.as_sum.entity.StoreInfo;
import java.util.List;

/**
 * Created by ran on 2016/8/25.
 */
public class ExpandListVIewAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<StoreInfo> storeInfos;

    public ExpandListVIewAdapter(Context context, List<StoreInfo> storeInfos) {
        this.context = context;
        this.storeInfos = storeInfos;
    }

    @Override
    public int getGroupCount() {
//        Log.e("--->","父布局个数"+storeInfos.size());
        return storeInfos.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
//        Log.e("--->","父布局id:"+groupPosition+",其子布局的个数:"+storeInfos.get(groupPosition).getShopInfos().size());
        return storeInfos.get(groupPosition).getShopInfos().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return storeInfos.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return storeInfos.get(groupPosition).getShopInfos().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holderGroup;
        StoreInfo storeInfo=(StoreInfo) getGroup(groupPosition);
        if (convertView == null) {
            holderGroup = new ViewHolderGroup();
            convertView = LayoutInflater.from(context).inflate(R.layout.expand_group, parent, false);
            holderGroup.tv_change=(TextView) convertView.findViewById(R.id.expand_item_tv_change);
            holderGroup.tv_name=(TextView) convertView.findViewById(R.id.expand_item_tv_name);
            holderGroup.store_img_check=(CheckBox) convertView.findViewById(R.id.expand_item_img_check);
            convertView.setTag(holderGroup);
        }else{
            holderGroup=(ViewHolderGroup)convertView.getTag();
        }
        holderGroup.tv_name.setText(storeInfo.getName());
        holderGroup.tv_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ShopInfo shopInfo=(ShopInfo) getChild(groupPosition,childPosition);
        ViewHodlerChild hodlerChild;
        if (convertView == null) {
                //一般商品布局
            hodlerChild = new ViewHodlerChild();
            convertView = LayoutInflater.from(context).inflate(R.layout.expand_sum, parent, false);
            hodlerChild.layout=(LinearLayout) convertView.findViewById(R.id.expand_item_layout);
            hodlerChild.tv_sum=(TextView) convertView.findViewById(R.id.expand_sum_tv);
            hodlerChild.tv_info=(TextView) convertView.findViewById(R.id.expand_sum_shop_info);
            hodlerChild.tv_price=(TextView) convertView.findViewById(R.id.expand_sum_shop_price);
            hodlerChild.shop_img_check=(CheckBox) convertView.findViewById(R.id.expand_sum_img_check);
            convertView.setTag(hodlerChild);
        }else{
            hodlerChild=(ViewHodlerChild)convertView.getTag();
        }
        if(isLastChild){
            hodlerChild.layout.setVisibility(View.VISIBLE);
        }else{
            hodlerChild.layout.setVisibility(View.GONE);
        }
        hodlerChild.tv_sum.setText((shopInfo.getPrice()+50)+"$");
        hodlerChild.tv_info.setText(shopInfo.getSinfo());
        hodlerChild.tv_price.setText(shopInfo.getPrice() + "￥");
        hodlerChild.shop_img_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ViewHolderGroup {
        TextView tv_name, tv_change;
        CheckBox store_img_check;
    }

    class ViewHodlerChild {
        TextView tv_price, tv_info,tv_sum;
        CheckBox shop_img_check;
        LinearLayout layout;
    }
}
